package com.ruoyi.wms.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.wms.domain.WmsCustom;
import com.ruoyi.wms.domain.WmsGoods;
import com.ruoyi.wms.mapper.WmsCustomMapper;
import com.ruoyi.wms.service.IWmsCustomService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 自定义模板Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-15
 */
@Service
public class WmsCustomServiceImpl implements IWmsCustomService {
  @Autowired private WmsCustomMapper wmsCustomMapper;

  /**
   * 查询自定义模板
   *
   * @param id 自定义模板主键
   * @return 自定义模板
   */
  @Override
  public WmsCustom selectWmsCustomById(Long id) {
    return wmsCustomMapper.selectWmsCustomById(id);
  }

  /**
   * 查询自定义模板列表
   *
   * @param wmsCustom 自定义模板
   * @return 自定义模板
   */
  @Override
  public List<WmsCustom> selectWmsCustomList(WmsCustom wmsCustom) {
    return wmsCustomMapper.selectWmsCustomList(wmsCustom);
  }

  /**
   * 新增自定义模板
   *
   * @param wmsCustom 自定义模板
   * @return 结果
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int insertWmsCustom(WmsCustom wmsCustom) {
    wmsCustom.setCreateTime(DateUtils.getNowDate());
    int rows = wmsCustomMapper.insertWmsCustom(wmsCustom);
    insertWmsGoods(wmsCustom);
    return rows;
  }

  /**
   * 修改自定义模板
   *
   * @param wmsCustom 自定义模板
   * @return 结果
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int updateWmsCustom(WmsCustom wmsCustom) {
    wmsCustom.setUpdateTime(DateUtils.getNowDate());
    wmsCustomMapper.deleteWmsGoodsByCustomId(wmsCustom.getId());
    insertWmsGoods(wmsCustom);
    return wmsCustomMapper.updateWmsCustom(wmsCustom);
  }

  /**
   * 批量删除自定义模板
   *
   * @param ids 需要删除的自定义模板主键
   * @return 结果
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int deleteWmsCustomByIds(Long[] ids) {
    wmsCustomMapper.deleteWmsGoodsByCustomIds(ids);
    return wmsCustomMapper.deleteWmsCustomByIds(ids);
  }

  /**
   * 删除自定义模板信息
   *
   * @param id 自定义模板主键
   * @return 结果
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int deleteWmsCustomById(Long id) {
    wmsCustomMapper.deleteWmsGoodsByCustomId(id);
    return wmsCustomMapper.deleteWmsCustomById(id);
  }

  /**
   * 新增商品信息
   *
   * @param wmsCustom 自定义模板对象
   */
  public void insertWmsGoods(WmsCustom wmsCustom) {
    List<WmsGoods> wmsGoodsList = wmsCustom.getWmsGoodsList();
    Long id = wmsCustom.getId();
    if (StringUtils.isNotNull(wmsGoodsList)) {
      List<WmsGoods> list = new ArrayList<WmsGoods>();
      for (WmsGoods wmsGoods : wmsGoodsList) {
        wmsGoods.setCustomId(id);
        list.add(wmsGoods);
      }
      if (list.size() > 0) {
        wmsCustomMapper.batchWmsGoods(list);
      }
    }
  }
}
